package com.forum.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.forum.common.utils.R;
import com.forum.community.entity.ForumCommunity;
import com.forum.community.entity.ForumCommunityUser;
import com.forum.community.service.ForumCommunityService;
import com.forum.community.service.ForumCommunityUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bncgnc
 * @since 2022-01-04
 */
@RestController
@RequestMapping("/community/forumCommunityUser")
public class ForumCommunityUserController {



    @Autowired
    ForumCommunityService communityService;

    @Autowired
    ForumCommunityUserService communityUserService;

    //当点击加入社区按钮的时候，就加入该用户id和社区id到表内，得有一个user?
    @PostMapping("addUser")
    public R addUser(@RequestBody ForumCommunityUser forumCommunityUser){

        //用户模块添加用户

        boolean i = communityUserService.save(forumCommunityUser);
        if(i==true){
            return R.ok("加入社区成功！");
        }
        return R.error("加入社区失败！");
    }

    //用户退出社区
    @DeleteMapping("deleteUser")
    public R deleteUser(@RequestBody ForumCommunityUser forumCommunityUser){
        QueryWrapper<ForumCommunityUser> query = new QueryWrapper<>();
        query.eq("user_id",forumCommunityUser.getUserId()).eq("community_id",forumCommunityUser.getCommunityId());
        boolean i = communityUserService.remove(query);
        if(i==true){
            return R.ok("退出社区成功！");
        }
        return R.error("退出社区失败！");
    }
    //列出社区中所有的用户,用户数communityUser.size
    @GetMapping("listCommunityUser/{communityId}")
    public R listCommunityUser(@PathVariable Long communityId){
        QueryWrapper<ForumCommunityUser> query = new QueryWrapper<>();
        query.eq("community_id",communityId);
        List<ForumCommunityUser> communityUser = communityUserService.list(query);


        //远程调用用户模块的根据id查询所有社区成员的数据


        return R.ok().put("communityUser",communityUser);
    }

    //查询出用户加入的所有社区
    @GetMapping("findAllCommunityById/{userId}")
    public R findAllCommunityById(@PathVariable Long userId){
        QueryWrapper<ForumCommunityUser> query = new QueryWrapper<>();
        query.eq("user_id",userId);
        List<ForumCommunityUser> list = communityUserService.list(query);

        List<ForumCommunity> finallist = new ArrayList<>();
        for(int i = 0;i<list.size();i++){
            ForumCommunity community = communityService.getById(list.get(i).getCommunityId());
            finallist.add(community);
        }

        return R.ok().put("userCommunityList",list);
    }

}

